<?php
/**
 * Created by PhpStorm.
 * User: 游贤川
 * Date: 2021/6/18
 * Time: 19:02
 */
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\RcUser;
use App\Models\Retained;
use App\Models\UserHelp;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;

class RcUserController extends Controller
{
    public function RcUser(Request $request)
    {
        //验证
        $verify = [
            'open_id'=>'required',
            'name'=>'required',
            'img'=>'required',
        ];
        $regulation = [
            'code.required' => 'open_id必填',
            'name.required' => '昵称必填',
            'img.required' => '头像必填',
        ];
        $validator = Validator::make($request->all(),$verify ,$regulation );
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data = $request->input();
        if ($data['card']==1||$data['time']==1){
            $data['sum'] = 0;
        }
        //判断用户是否存在
        Log::info('用户数据',$data);
        $user = RcUser::query()->updateOrCreate(['open_id'=>$data['open_id']],$data);
        $user = RcUser::query()->with('retained')->where(['open_id'=>$data['open_id']])->first();
        $user->isExist =$user->retained?1: 0;
        $user->rank = 0;
        if ($user->time!=1 && $user->card!=1){
            $sql = 'SELECT b.rownum FROM(SELECT t.*, @rownum := @rownum + 1 AS rownum FROM (SELECT @rownum := 0) r,(SELECT * FROM rc_user ORDER BY `sum` DESC) AS t WHERE `time` != 1 AND `card` != 1) AS b WHERE b.open_id = ?';
            $rank_model = DB::select($sql,[$data['open_id']]);
            $rank_num = intval($rank_model[0]->rownum);
            $user->rank = $rank_num;
        }
        return $this->success($user,'成功');
    }

    public function HelpUser(Request $request)
    {
        return $this->error('活动已结束');
        //验证
        $verify = [
            'open_id'=>'required',
            'parent_openid'=>'required',
        ];
        $regulation = [
            'open_id.required' => 'open_id必填',
            'parent_openid.required' => '助力对象openid必填',
        ];
        $validator = Validator::make($request->all(),$verify ,$regulation );
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data = $request->input();
        $user_openid = RcUser::query()->where(['open_id'=>$data['open_id']])->first();
        if (!$user_openid){
            return $this->error('用户不存在');
        }
        $parent_openid = RcUser::query()->where(['open_id'=>$data['parent_openid']])->first();
        if (!$parent_openid){
            return $this->error('邀请对象不存在');
        }
        //判断用户当日是否点过赞
        $sta_time = Carbon::now()->startOfDay();
        $end_time = Carbon::now()->endOfDay();
        $time_now = UserHelp::query()->where(['user_openid'=>$data['open_id']])->whereBetween('created_at',[$sta_time,$end_time])->count();
        if (!$time_now){
            //判断是否为新用户添加对应积分
            $help_data = ['user_openid'=>$data['open_id'],'parent_openid'=>$data['parent_openid'],'num'=>10];
            $help = UserHelp::Create($help_data);
           RcUser::query()->where(['open_id'=>$data['parent_openid']])->increment('sum',10);
        }else{
            return $this->error('每人每天只有一次助力机会，您今日的助力次数已用完～');
        }
        return $this->success($help,'助力成功');
    }

    public function RankList()
    {
//        $help_list = RcUser::query()->whereNotNull('time')->whereNotNull('card')->orderByDesc('sum')->get();
        $help_list = RcUser::query()->where([['time','!=',1],['card','!=',1]])->orderByDesc('sum')->get();
        $help_list = $help_list->take(20);
        return $this->success($help_list,'成功');
    }

    public function HelpList(Request $request)
    {
        //验证
        $verify = [
            'open_id'=>'required',
        ];
        $regulation = [
            'open_id.required' => 'open_id必填',
        ];
        $validator = Validator::make($request->all(),$verify ,$regulation );
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data = $request->input();
        $user = RcUser::query()->where(['open_id'=>$data['open_id']])->first();
        if (!$user){
            return $this->error('用户不存在');
        }
        $list = UserHelp::query()->where(['parent_openid'=>$data['open_id']])->select(['created_at','num'])->orderByDesc('id')->get()->take(20);

        return $this->success($list,'成功');
    }

    public function AddRetained(Request $request)
    {
        //验证
        $verify = [
            'user_openid'=>'required',
        ];
        $regulation = [
            'user_openid.required' => 'open_id必填',
        ];
        $validator = Validator::make($request->all(),$verify ,$regulation );
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data = $request->input();
        $user = RcUser::query()->with('retained')->where(['open_id'=>$data['user_openid']])->first();
        if (!$user){
            return $this->error('用户不存在');
        }
        if ($user->retained){
            return $this->error('用户已留资！');
        }
        $model = Retained::Create($data);

        return $this->success($model,'留资成功！');
    }
}